package com.dalin.framework.common.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/*****************************************************************************
 * @content: 网络编程工具                                                                                                                                                       *
 * @author : 那建林                                                                                                                                                                  *
 * @crtDate: 2010-03-01                                                     *
 ****************************************************************************/
public final class NetUtils {
	private NetUtils() {}
	private static final Log log = LogFactory.getLog(NetUtils.class);
	
	/**
	 * linux下能否ping通ＩＰ
	 */
	public static boolean ping(String ip)  throws Exception {
		boolean rtn = true;
		Process process = Runtime.getRuntime().exec("ping -c1 -w1 " + ip);
		InputStream is = process.getInputStream();
    	InputStreamReader inputReader = new InputStreamReader(
    			is);
        BufferedReader lineReader = new BufferedReader(inputReader);
        String line;
        int index = -1;
        while ((line = lineReader.readLine()) != null){
        	if (line.contains("0 received")) {
        		rtn = false;
        		break;
        	} else {
        	//	System.out.println("通的："+ip);
        	}
        }
        is.close();
        lineReader.close();
        inputReader.close();
        return rtn;
	}
}